package com.bootdo.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.system.dao.DlTradeClassDao;
import com.bootdo.system.domain.DlTradeClassDO;
import com.bootdo.system.service.DlTradeClassService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.system.domain.DlTradeDO;
import com.bootdo.system.service.DlTradeService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2021-05-31 09:49:17
 */
 
@Controller
@RequestMapping("/system/dlTrade")
public class DlTradeController {
	@Autowired
	private DlTradeService dlTradeService;
	@Autowired
	private DlTradeClassService dlTradeClassService;

	@GetMapping()
	@RequiresPermissions("system:dlTrade:dlTrade")
	String DlTrade(){
	    return "system/dlTrade/dlTrade";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:dlTrade:dlTrade")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<DlTradeDO> dlTradeList = dlTradeService.list(query);
		int count = dlTradeService.count(query);
		PageUtils pageUtils = new PageUtils(dlTradeList, count);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("system:dlTrade:add")
	String add(Model model){
		List<DlTradeClassDO> list = dlTradeClassService.list(new HashMap<>());
		model.addAttribute("list", list);
		return "system/dlTrade/add";
	}

	@GetMapping("/edit/{tradeId}")
	@RequiresPermissions("system:dlTrade:edit")
	String edit(@PathVariable("tradeId") Integer tradeId,Model model){
		DlTradeDO dlTrade = dlTradeService.get(tradeId);
		List<DlTradeClassDO> list = dlTradeClassService.list(new HashMap<>());
		model.addAttribute("list", list);
		model.addAttribute("dlTrade", dlTrade);
	    return "system/dlTrade/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:dlTrade:add")
	public R save( DlTradeDO dlTrade){
		if(dlTradeService.save(dlTrade)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:dlTrade:edit")
	public R update( DlTradeDO dlTrade){
		dlTradeService.update(dlTrade);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:dlTrade:remove")
	public R remove( Integer tradeId){
		if(dlTradeService.remove(tradeId)>0){
			return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:dlTrade:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] tradeIds){
		dlTradeService.batchRemove(tradeIds);
		return R.ok();
	}
	
}
